<?php

namespace App\ApiBackend\Menu;

use App\Http\Controllers\ApiBaseController;
use Illuminate\Support\Facades\DB;

/**
 *  菜单列表
 */
class Page extends ApiBaseController
{
  /**
   * 参数检查
   */
  protected function check(): void
  {

  }

  /**
   * 业务主体
   * 最大深度为4级
   */
  protected function service(): void
  {
    $typeTextArr = [
      0 => "栏目",
      1 => "菜单",
      2 => "权限",
    ];
    //基础查询构造器
    $selectFields = [
      "*",
    ];
    $listData = DB::table('admin_menu')
      ->select($selectFields)
      ->where('pid', 0)
      ->orderBy("orderNo", "DESC")
      ->get();
    foreach ($listData as &$item) {
      $item->typeText = $typeTextArr[$item->type];
      $item->children = DB::table('admin_menu')
        ->select($selectFields)
        ->where('pid', $item->id)
        ->orderBy("orderNo", "DESC")
        ->get();
      foreach ($item->children as &$childItem) {
        $childItem->typeText = $typeTextArr[$childItem->type];
        $childItem->children = DB::table('admin_menu')
          ->select($selectFields)
          ->where('pid', $childItem->id)
          ->orderBy("orderNo", "DESC")
          ->get();

        foreach ($childItem->children as &$childOneItem) {
          $childOneItem->typeText = $typeTextArr[$childOneItem->type];
          $childOneItem->children = DB::table('admin_menu')
            ->select($selectFields)
            ->where('pid', $childOneItem->id)
            ->orderBy("orderNo", "DESC")
            ->get();
          foreach ($childOneItem->children as &$childTwoItem) {
            $childTwoItem->typeText = $typeTextArr[$childTwoItem->type];
          }
        }
      }
    }

    empty($listData) && $listData = [];

    $this->result["data"] = $listData;
    return;
  }
}
